using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Action.Entity;
using T.FAS.Runtime.Action.Service;
using T.FAS.Runtime.Action.SGSPI;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.Action.SGPlugin
{
    [SGService]
    public class ActionSGService: IActionSGService
    {
        /// <summary>
        /// DQ执行
        /// </summary>
        /// <param name="dqId"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [SGService(ServiceID = "FASR-DataQueryExecute")]
        public TResult DQExecute(string dqId, List<ActionParameter> parameters)
        {
            if (dqId == null)
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError, CommonStringClassDonotTranslated.ActionSGService_DQExecute);
            }

            var originalParams = new RemoteInvokeContext();
            originalParams.AddParam("dqId",dqId);
            originalParams.AddParam("parameters", parameters);

            var result = ActionExecuteService.GetInstance.Execute(dqId, ActionType.DQ ,parameters,remoteInovkeContext: originalParams);
            return result;
        }
        /// <summary>
        /// Action执行
        /// </summary>
        /// <param name="actionId"></param>
        /// <param name="parameters"></param>
        /// <returns>ViewModelResult</returns>
        public TResult Execute(string actionId, List<ActionParameter> parameters)
        {
            if (actionId == null)
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,CommonStringClassDonotTranslated.ActionSGService_Execute);
            }

            var originalParams = new RemoteInvokeContext();
            originalParams.AddParam("actionId", actionId);
            originalParams.AddParam("parameters", parameters);

            var result = ActionExecuteService.GetInstance.Execute(actionId, ActionType.Action,parameters, remoteInovkeContext: originalParams);
            return result;
        }
        /// <summary>
        /// CDO执行
        /// </summary>
        /// <param name="cdoId"></param>
        /// <param name="actionType"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public TResult CDOExecute(string cdoId, SPI.ActionType actionType,List<ActionParameter> parameters)
        {
            if (string.IsNullOrEmpty(cdoId))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,string.Format(CommonStringClassDonotTranslated.ActionSGService_CDOExecute));
            }

            var originalParams = new RemoteInvokeContext();
            originalParams.AddParam("cdoId", cdoId);
            originalParams.AddParam("actionType", actionType);
            originalParams.AddParam("parameters", parameters);

            var result = ActionExecuteService.GetInstance.Execute(cdoId,actionType,parameters,true, remoteInovkeContext: originalParams);
            return result;
        }
        /// <summary>
        /// 外部系统用于通过低代码CDO获取数据信息
        /// </summary>
        /// <param name="cdoCode"></param>
        /// <param name="dataID"></param>
        /// <returns></returns>
        public TResult GetData(string cdoCode, string dataID)
        {
            if (string.IsNullOrEmpty(cdoCode))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError, CommonStringClassDonotTranslated.ActionSGService_GetData);
            }
            if (string.IsNullOrEmpty(dataID))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError, CommonStringClassDonotTranslated.ActionSGService_GetData1);
            }
            var paramList = new List<ActionParameter>();
            paramList.Add(new ActionParameter()
            {
                Order =0,
                ParameterName ="ID",
                ParameterValue =dataID,
            });

            var originalParams = new RemoteInvokeContext();
            originalParams.AddParam("cdoCode", cdoCode);
            originalParams.AddParam("dataID", dataID);
            var result = ActionExecuteService.GetInstance.Execute(cdoCode, ActionType.CDO_GetByID, paramList);
            return result;
        }
        /// <summary>
        /// AF执行
        /// </summary>
        /// <param name="afID"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public TResult AFExecute(string metaId,string actionCode, List<ActionParameter> parameters)
        {
            if (string.IsNullOrEmpty(metaId))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,string.Format(CommonStringClassDonotTranslated.ActionSGService_AFExecute));
            }
            if (string.IsNullOrEmpty(actionCode))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,string.Format(CommonStringClassDonotTranslated.ActionSGService_AFExecute1));
            }
            var result = ActionExecuteService.GetInstance.Execute(metaId,actionCode,parameters,true);
            return result;
        }

        /// <summary>
        /// 根据ActionID和DQ列表获取所有Action元数据及返回值TSchema信息
        /// </summary>
        /// <param name="actionIds"></param>
        /// <returns></returns>
        public List<ActionInfoWithSchema> GetAllInfoWithSchema(string filter)
        {
            if (string.IsNullOrEmpty(filter))
            {
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,CommonStringClassDonotTranslated.ActionSGService_GetAllInfoWithSchema);
            }
            var dic = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(filter);
            if (dic == null)
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,CommonStringClassDonotTranslated.ActionSGService_GetAllInfoWithSchema1);
            var result = new ActionRTPService().GetActionInfoWithSchema(dic);
            return result;
        }

        /// <summary>
        /// 根据ActionID列表获取Action元数据及返回值TSchema信息
        /// </summary>
        /// <param name="actionIds"></param>
        /// <returns></returns>
        public List<ActionInfoWithSchema> GetActionInfoWithSchema(List<string> actionIds)
        {
            if (actionIds == null || actionIds.Count <= 0)
                throw new FasException(ActionExecuteExceptionCode.ActionExecuteParamNullError,CommonStringClassDonotTranslated.ActionSGService_GetActionInfoWithSchema);
            var result = new ActionRTPService().GetActionInfoWithSchema(actionIds);
            return result;
        }
        /// <summary>
        /// 获取给定的DataBase的Schema信息
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public List<DataBaseInfoView> GetDBInfoViews(List<string> dbCodes)
        {
            if(dbCodes == null)
                throw new FasException(ActionExecuteExceptionCode.ActionDBInfoHelperError, CommonStringClassDonotTranslated.ActionSGService_GetDBInfoViews);
            var result = new ActionRTPHelpService().GetDBInfoViews(dbCodes);
            return result;
        }
    }
}
